package com.atguigu.MyTest;



import java.sql.SQLException;
import java.util.List;

/**
 * @author haoranlu
 * @project IntelliJ IDEA
 */
public class BookDAOImp1 extends BasicDAO<Books> implements BookDAO{
    @Override
    public void addBook(Books book) {
        String sql = "insert into books values(null,?,?,?,?,?,?)";
        try {
            update(sql, book.getTitle(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteBookById(String id) {
        String sql = "delete from books where id = ?";
        try {
            update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Books getBookById(String id) {
        String sql = "select id,title,author,price,sales,stock,img_path as imgPath from books where id = ?";
        Books b = getBean(sql, id);
        return b;
    }

    @Override
    public Books updateBookById(String id) {
        String sql = "update books set title=?,author=?,price=?,sales=?,stock=?,img_path=? where id = ?";
        Books b = getBookById(id);
        try {
            update(sql,b.getTitle(),b.getAuthor(),b.getPrice(),b.getSales(),b.getStock(),b.getImgPath());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return b;
    }

    @Override
    public void updateBook(Books book) {
        String sql = "update books set title=?,author=?,price=?,sales=?,stock=?,img_path =? where id = ?";
        try {
            update(sql,book.getTitle(),book.getAuthor(),book.getPrice(), book.getSales(),book.getStock(),book.getImgPath(),book.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Books> getAllBook() {
        String sql = "select id,title,author,price,sales,stock,img_path as imgPath from books";
        List<Books> list = null;
        list = getBeanList(sql);
        return list;
    }

    @Override
    public long getBooksCount() {
        long counts = 0;
        String sql = "select count(*) from books";
        counts = (long)getObject(sql);
        return counts;
    }


    @Override
    public Page<Books> getPagesBook(Page<Books> page) {
        String sql = "select id,title,author,price,sales,stock,img_path as imgPath from books limit ?,?";
        int pageNo = page.getPageNo();
        int pageSize = Page.PAGE_SIZE;
        List<Books> list = getBeanList(sql, (pageNo -1)* pageSize, pageSize);
        page.setTotalRecord((int)getBooksCount());
        page.setList(list);
        return page;
    }

    @Override
    public Page<Books> getPageBooksByPrice(Page<Books> page, double minPrice, double maxPrice) {
        String sql = "SELECT id,title,author,price,sales,stock,img_path as imgPath FROM books WHERE Price > ? AND Price < ? LIMIT ?, ?";
        int pageNo = page.getPageNo();
        int pageSize = Page.PAGE_SIZE;
        List<Books> list = getBeanList(sql,minPrice,maxPrice,(pageNo -1)* pageSize, pageSize);
        page.setTotalRecord(list.size());
        page.setList(list);
        return page;
    }

    @Override
    public void batchUpdateSalesAndStock(Object[][] params) {
        String sql = "update books set sales=?,stock=? where id = ?";
        batchUpdate(sql,params);
    }
}
